﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;

namespace PPAP.Web.Todolist
{
    public partial class Approval : PageBase
    {
        SamplecardBO sbo = new SamplecardBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            DataSet ds = sbo.GetList(" 1=1 and ((s.STATUS=1  and s.status!=9  and( u.superior1='" + CurrentUser.Id + "' or u.superior2='" + CurrentUser.Id + "') )or (s.STATUS=4  and s.status!=9  and( u.superior1='" + CurrentUser.Id + "'))or (s.STATUS=5  and s.status!=9  and( u.superior2='" + CurrentUser.Id + "'))) ");
            AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
            dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            dgList.DataBind();
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        public string Status(string status)
        {
            string font = "";
            switch (status)
            {
                case "0":
                    font = "Created";
                    break;
                case "1":
                case "4":
                case "5":
                    font = "Circulating";
                    break;
                case "2":
                    font = "Circulating(<font color='red'>Reject</font>)";
                    break;
                default:
                    font = "Decided";
                    break;
            }
            return font;

        }

        protected void dgList_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.dgList.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.dgList.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.dgList.Attributes["SortExpression"] = sortExpression;
            this.dgList.Attributes["SortDirection"] = sortDirection;


            string pic = "";
            pic = sortDirection == "ASC" ? "<img src='../Images/icon/arrowup.gif' />" : "<img src='../Images/icon/arrowdown.gif' />";

            foreach (DataGridColumn item in dgList.Columns)
            {
                if (item.SortExpression == sortExpression)
                {
                    item.HeaderText = item.FooterText + " " + pic;
                }
                else
                    item.HeaderText = item.FooterText;
            }

            this.BindGridView();

        }

        private void BindGridView()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.dgList.Attributes["SortExpression"];
            string sortDirection = this.dgList.Attributes["SortDirection"];
            if (sortExpression != null)
            {
                // 调用业务数据获取方法
                string sort = string.Format("  order by lower({0})  {1} ", sortExpression, sortDirection);
                if (sortExpression == "STARTTIME")
                {
                    sort = string.Format("  order by {0}  {1} ", sortExpression, sortDirection);
                }
                DataTable dtBind = sbo.GetList(" s.STATUS=1 and s.status!=9  and u.superior='"+CurrentUser.Id+"'"+ sort).Tables[0];
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }
                // GridView绑定并显示数据
                this.dgList.DataSource = GetPageTable(dtBind, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
                this.dgList.DataBind();
            }
            else
                LoadData();
        }
    }
}